import React from 'react';
import * as THREE from 'three';
import Scene from '../Scene';
import vertexShader from './shaders/fireflies/vertex.glsl';
import fragmentShader from './shaders/fireflies/fragment.glsl';
import style from '../FrozenStation.css';

export default class BakeModel {
    constructor(){
        this.scene = new Scene();
        this.canvasScene = this.scene.scene;
        this.resources = this.scene.resources;
        this.room = this.resources.items.room;
        this.renderer = this.scene.renderer;
        this.time = this.scene.time;
        this.sizes = this.scene.sizes;
        this.camera = this.scene.camera.perspectiveCamera;
        this.setModel();
    }
    setModel(){
        this.resources.items.bake.flipY = false;
        // 设置纹理对象的色彩空间，保证跟烘焙图的色彩空间一致都是sRGB;
        this.resources.items.bake.encoding = THREE.sRGBEncoding;
        let mtl = new THREE.MeshBasicMaterial({
            map:this.resources.items.bake
        });
        this.room.scene.traverse((child)=>{
            child.material = mtl;
        })
        this.canvasScene.add(this.room.scene); 
        // 添加分散点几何体，位置随机分布
        let dotGeometry = new THREE.BufferGeometry();
        let dotMount = 300;
        let posArr = new Float32Array(dotMount * 3);
        for ( let i = 0; i < dotMount; i++ ) {
            posArr[i * 3 + 0] = ( Math.random() - 0.5 ) * 4;
            posArr[i * 3 + 1] = Math.random() * 2;
            posArr[i * 3 + 2] = ( Math.random() - 0.5) * 4 ;
        }
        dotGeometry.setAttribute('position', new THREE.BufferAttribute(posArr, 3))
        // let dotMtl = new THREE.PointsMaterial({ size:1, sizeAttenuation:true });
        let dotMtl = new THREE.ShaderMaterial({
            vertexShader,
            fragmentShader,
            uniforms:{
                // uPixelRatio:{ setAttribute, setPixelRatio,   }
                uPixelRatio:{ value:this.sizes.pixelRatio },
                uSize:{ value:4 }
            }
        })
        let dots = new THREE.Points(dotGeometry, dotMtl);
        // this.canvasScene.add(dots);
    }
    update(){
        
    }
}